﻿using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore.Diagnostics;
using PHMEE.ToolKits;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.Identity;
using Volo.Abp.PermissionManagement;
using IdentityUser = Volo.Abp.Identity.IdentityUser;

namespace PHMEE.DbMigrator
{
    public class PHMEEDataSeedContributor : IDataSeedContributor, ITransientDependency
    {
        private readonly IGuidGenerator guidGenerator;
        private readonly IdentityUserManager identityUserManager;
        private readonly IdentityRoleManager identityRoleManager;
        private readonly IPermissionDataSeeder permissionDataSeeder;

        public PHMEEDataSeedContributor(IGuidGenerator guidGenerator, IdentityUserManager identityUserManager,
        IdentityRoleManager identityRoleManager, IPermissionDataSeeder permissionDataSeeder)
        {
            this.guidGenerator = guidGenerator;
            this.identityUserManager = identityUserManager;
            this.identityRoleManager = identityRoleManager;
            this.permissionDataSeeder = permissionDataSeeder;
        }

        public async Task SeedAsync(DataSeedContext context)
        {
            await SeedRoleUserAsync();
        }

        /// <summary>
        /// 后续业务确定了再确定每个角色有哪些操作权限或者什么操作权限都不给
        /// </summary>
        /// <returns></returns>
        private async Task SeedRoleUserAsync()
        {

            List<IdentityRole> roles =
            [
                new IdentityRole(guidGenerator.Create(), RoleEnum.Captain.ToString().ToLower())
                {
                    IsStatic = true,
                    IsPublic = true,
                },
                //new IdentityRole(guidGenerator.Create(), RoleEnum.ChiefOfficer.ToString())
                //{
                //    IsStatic = true,
                //    IsPublic = true,
                //},
                //new IdentityRole(guidGenerator.Create(), RoleEnum.SecondOfficer.ToString())
                //{
                //    IsStatic = true,
                //    IsPublic = true,
                //},
                //new IdentityRole(guidGenerator.Create(), RoleEnum.ThirdOfficer.ToString())
                //{
                //    IsStatic = true,
                //    IsPublic = true,
                //},
                //new IdentityRole(guidGenerator.Create(), RoleEnum.ChiefEngineer.ToString())
                //{
                //    IsStatic = true,
                //    IsPublic = true,
                //},
                //new IdentityRole(guidGenerator.Create(), RoleEnum.SecondEngineer.ToString())
                //{
                //    IsStatic = true,
                //    IsPublic = true,
                //},
                //new IdentityRole(guidGenerator.Create(), RoleEnum.ThirdEngineer.ToString())
                //{
                //    IsStatic = true,
                //    IsPublic = true,
                //},

                //new IdentityRole(guidGenerator.Create(), RoleEnum.ElectroTechnicalOfficer.ToString())
                //{
                //    IsStatic = true,
                //    IsPublic = true,
                //},
                //new IdentityRole(guidGenerator.Create(), RoleEnum.AbleSeaman.ToString())
                //{
                //    IsStatic = true,
                //    IsPublic = true,
                //},
                //new IdentityRole(guidGenerator.Create(), RoleEnum.SafetyOfficer.ToString())
                //{
                //    IsStatic = true,
                //    IsPublic = true,
                //},
                //new IdentityRole(guidGenerator.Create(), RoleEnum.EngineRoomRating.ToString())
                //{
                //    IsStatic = true,
                //    IsPublic = true,
                //},

            ];

            string password = "123456";

            foreach (var role in roles)
            {
                if (!await identityRoleManager.RoleExistsAsync(role.Name))
                {
                    (await identityRoleManager.CreateAsync(role)).CheckErrors();

                    await permissionDataSeeder.SeedAsync(RolePermissionValueProvider.ProviderName, role.Name, []);

                    var tenantUser = await identityUserManager.FindByNameAsync(role.Name);

                    if (tenantUser == null)
                    {
                        RoleEnum roleEnum = (RoleEnum)Enum.Parse(typeof(RoleEnum), role.Name, true);
                        tenantUser = new IdentityUser(guidGenerator.Create(), role.Name.ToLower(), $"{role.Name}@abp.io")
                        {
                            Name = PHMEECoreHelper.PhmeeGetDescription(roleEnum),
                        };

                        tenantUser.AddRole(role.Id);

                        (await identityUserManager.CreateAsync(tenantUser)).CheckErrors();
                        (await identityUserManager.AddPasswordAsync(tenantUser, password)).CheckErrors();
                    }
                }
            }
        }
    }

    public enum RoleEnum
    {
        /// <summary>
        /// 船长
        /// 负责全船的管理、航行、安全和运营
        /// </summary>
        [Description("船长")]
        Captain = 1,

        /// <summary>
        /// 大副
        /// 负责甲板上的主要操作，管理船员和货物
        /// </summary>
        [Description("大副")]
        ChiefOfficer = 2,

        /// <summary>
        /// 二副
        /// 负责导航、航行计划和航行记录
        /// </summary>
        [Description("二副")]
        SecondOfficer = 3,

        /// <summary>
        /// 三副
        /// 协助导航工作，负责安全设备检查
        /// </summary>
        [Description("三副")]
        ThirdOfficer = 4,

        /// <summary>
        /// 轮机长
        /// 负责船上的机械设备，包括主机和发电机的维护和操作
        /// </summary>
        [Description("轮机长")]
        ChiefEngineer = 5,

        /// <summary>
        /// 二轮机长
        /// 协助轮机长进行设备维护和操作
        /// </summary>
        [Description("二轮机长")]
        SecondEngineer = 6,

        /// <summary>
        /// 三轮机长
        /// 协助二轮机进行设备的日常维护和检查
        /// </summary>
        [Description("三轮机长")]
        ThirdEngineer = 7,

        /// <summary>
        /// 电机员
        /// 负责船上电气设备的维护和操作
        /// </summary>
        [Description("电机员")]
        ElectroTechnicalOfficer = 8,

        /// <summary>
        /// 水手
        /// 进行甲板上的各项工作，包括设备操作和货物装卸
        /// </summary>
        [Description("水手")]
        AbleSeaman = 9,

        /// <summary>
        /// 安全员
        /// 负责船上的安全管理和应急准备
        /// </summary>
        [Description("安全员")]
        SafetyOfficer = 10,

        /// <summary>
        /// 机务员
        /// 协助轮机部进行日常维护和检查，通常包括机务工人
        /// </summary>
        [Description("机务员")]
        EngineRoomRating = 11,

    }
}
